import { MockMethod } from 'vite-plugin-mock'

export default [
  // RESTful 风格接口
  {
    url: '/api/users',
    method: 'get',
    response: () => ({
      code: 200,
      data: [
        { id: 1, name: 'Tom', age: 18 },
        { id: 2, name: 'Jerry', age: 22 }
      ]
    })
  },

  // 带参数的接口
  {
    url: '/api/user/:id',
    method: 'get',
    response: ({ query }) => ({
      code: 200,
      data: {
        id: query.id,
        name: 'Mock User',
        role: 'admin'
      }
    })
  },
  {
    url: '/api/user/register',
    method: 'post',
    timeout: 500, // 延迟500毫秒返回
    response: ({ body }) => {
      // phone, password, email
      const { phone } = body;
      console.log('eeeeeeeeeeee', body, body.phone)
      // 这里简单模拟注册成功，实际中可能需要检查参数
      // if (!phone || !password) {
      //   return {
      //     code: 400,
      //     message: '参数错误'
      //   };
      // }

      // 模拟返回注册成功信息
      return {
        code: 200,
        message: '注册成功',
        data: {
          id: '@guid', // 使用mockjs的语法生成一个guid
          phone,
          token: '@guid' // 生成一个token
        }
      };
    },
  },
  // POST 请求
  {
    url: '/api/user/login',
    method: 'post',
    timeout: 1000, // 模拟延迟
    response: ({ query, body }) => {
      console.log(body, 'aaaaaaaaaaaaaaa', query)
      if (body.username === '13420299300' && body.password === '2222wwqweqw') {
        return {
          code: 200,
          data: {
            token: 'MOCK_TOKEN_123456'
          }
        }
      }

      return {
        code: 200,
        data: {
          token: 'MOCK_TOKEN_123456'
        }
      }
      // return {
      //   code: 401,
      //   message: 'Invalid credentials'
      // }
    }
  }
] as MockMethod[]
